package com.gxuwz.dao.impl;

import com.gxuwz.dao.DepartmentDao;
import com.gxuwz.entity.Department;
import com.gxuwz.entity.vo.PageVo;
import com.gxuwz.result.PageInfo;
import com.gxuwz.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class DepartmentDaoImpl implements DepartmentDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public PageVo selectDepartmentAll(Department entity, PageInfo pageInfo) {
        String sql = "select * from sys_department where del_flag = 0 ";
        BeanPropertyRowMapper<Department> departmentBeanPropertyRowMapper = new BeanPropertyRowMapper<Department>(Department.class);
        List<Object> example = new ArrayList<Object>();
        PageUtil pageUtil = new PageUtil();
        if(entity==null){
            Object[] params = null;
            PageVo pageVo = pageUtil.queryForPage(sql, pageInfo.getPageNum(), pageInfo.getPageSize(), params, departmentBeanPropertyRowMapper,jdbcTemplate);
            return pageVo;
        } else {
            Object[] params;
            if(entity.getDepID() != null && !entity.getDepID().equals("")){
                sql += "and depID like ?";
                example.add("%"+entity.getDepID()+"%");
            }
            if(entity.getDepName() != null && !entity.getDepName().equals("")) {
                sql += "and depName like ?";
                example.add("%"+entity.getDepName()+"%");
            }
            params = example.toArray();
            PageVo pageVo = pageUtil.queryForPage(sql, pageInfo.getPageNum(), pageInfo.getPageSize(), params, departmentBeanPropertyRowMapper,jdbcTemplate);
            return pageVo;
        }
    }

    public Department selectDepartmentByID(String depId) {
        String sql = "select * from sys_department where del_flag = 0 and depID = ?";
        BeanPropertyRowMapper<Department> departmentBeanPropertyRowMapper = new BeanPropertyRowMapper<Department>(Department.class);
        Object[] params = new Object[]{depId};
        try{
            Department department = jdbcTemplate.queryForObject(sql, departmentBeanPropertyRowMapper, params);
            return department;
        } catch (Exception ex){
            return null;
        }
    }

    public int insertDepartment(Department entity) {
        String sql = "insert into sys_department values(?,?,0)";
        Object[] params = new Object[]{entity.getDepID(),entity.getDepName()};
        int insert = jdbcTemplate.update(sql, params);
        return insert;
    }

    public int updateDepartment(Department entity,String oldId) {
        String sql = "update sys_department set depID = ? , depName = ? where depID = ?";
        Object[] params = new Object[]{entity.getDepID(),entity.getDepName(),oldId};
        int update = jdbcTemplate.update(sql, params);
        return update;
    }

    public int deleteDepartmentById(String depId) {
        String sql = "update sys_department set del_flag = 1 where depID = ?";
        Object[] params = new Object[]{depId};
        int delete = jdbcTemplate.update(sql, params);
        return delete;
    }


    public List<String> selectDepartmentIds() {
        String sql = "select depID from sys_department ";
        List<String> ids = jdbcTemplate.queryForList(sql, String.class);
        return ids;
    }

    public List<Department> selectDepartmentIDAndName() {
        String sql = "select depID,depName from sys_department where del_flag = 0";
        BeanPropertyRowMapper<Department> departmentBeanPropertyRowMapper = new BeanPropertyRowMapper<Department>(Department.class);
        List<Department> departmentList = jdbcTemplate.query(sql, departmentBeanPropertyRowMapper);
        return departmentList;
    }

}
